/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var maxSlidingWindow = function (nums, k) {
    if (k === 1) return nums

    let tempIndexQueue = [],
        res = [];

    // 删除比当前索引的值小的左侧的值
    for (let i = 0; i < k; i++) {
        while (tempIndexQueue.length > 0 && nums[i] >= nums[tempIndexQueue[tempIndexQueue.length - 1]]) {
            tempIndexQueue.pop()
        }
        tempIndexQueue.push(i)
    }
    res.push(nums[tempIndexQueue[0]])

    for (let i = k; i < nums.length; i++) {
        while (tempIndexQueue.length > 0 && nums[i] >= nums[tempIndexQueue[tempIndexQueue.length - 1]]) {
            tempIndexQueue.pop()
        }
        tempIndexQueue.push(i)

        // 队首的索引超出了滑动窗口的左侧
        if (tempIndexQueue[0] <= i - k) {
            tempIndexQueue.shift()
        }

        res.push(nums[tempIndexQueue[0]])
    }

    return res
};